package com.n.Internet;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 以此类表示IP地址对象
 * 通过静态方法进行new对象操作，该类中的构造方法是私有的（通过静态方法进行实例化对象可以更好的控制构造对象）
 *
 * @author Mc.Chai
 * @program: Java基础
 * @create 2021-11-25-21:08 21:08
 */
public class TestInetAddress {
    public static void main(String[] args) throws UnknownHostException {
        //获得本机IP地址
        InetAddress localHost = InetAddress.getLocalHost();
        //获得IP地址字符串
        System.out.println(localHost.getAddress());
        System.out.println(localHost.getHostAddress());
        //获得IP地址对应主机名称
        System.out.println(localHost.getHostName());
        //获得任意主机的IP地址对象（IP、主机名、域名）
        InetAddress byName = InetAddress.getByName("www.baidu.com");
        System.out.println(byName.getHostName());
        System.out.println(byName.getHostAddress());

        //获得任意域名所绑定的所有的IP地址
        InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
        for (InetAddress name:
             allByName) {
            System.out.println(name.getHostName());
            System.out.println(name.getHostAddress());
        }
    }
}
